import request from '@/utils/request';
import { getCommon, getRegion, getLink, getStat, getCategoryList, getSanCatList } from '@/api/apis';

export default {
  namespaced: false,
  state: {
    common: {}, // 基本信息
    regions: {}, // 省市区数据
    links: [], // 友情链接
    stat: {}, // 底部统计
    store: {}, // 网站信息
    payment: {}, //支付配置
    categoryList: [], // 产品分类列表
    VUEX_sanCatList: [], // 三级分类列表
    isShowFeedbackModal: false, // 意见反馈弹窗控制
    isShowReportModal: false, // 一键举报弹窗控制
  },
  getters: {
    common: (state) => state.common,
    regions: (state) => state.regions,
    links: (state) => state.links,
    stat: (state) => state.stat,
    initStore: (state) => state.store,
    initPayment: (state) => state.payment,
    categoryList: (state) => state.categoryList,
    VUEX_sanCatList: (state) => state.VUEX_sanCatList,
    isShowFeedbackModal: (state) => state.isShowFeedbackModal,
    isShowReportModal: (state) => state.isShowReportModal,
  },
  actions: {
    // 初始化数据
    async appInit({ commit }) {
      const result = await getCommon();
      if (result.code === 1) {
        commit('CONFIG', result.data);
        return result.data;
      }
      return false;
    },
    // 省市区数据
    async getRegionList({ commit }) {
      const result = await getRegion();
      if (result.code === 1) {
        commit('SET_REGIONS', result.data);
        return result.data;
      }
      return false;
    },
    // 获取友情链接
    async getLinks({ commit }) {
      const result = await getLink();
      if (result.code === 1) {
        commit('SET_LINKS', result.data);
        return result.data;
      }
      return false;
    },
    // 获取底部统计
    async getStat({ commit }) {
      const result = await getStat();
      if (result.code === 1) {
        commit('SET_STAT', result.data);
        return result.data;
      }
      return false;
    },
    // 获取所有产品分类
    async getCategoryList({ commit }) {
      const result = await getCategoryList(); // 调用 API 获取分类列表
      if (result.code === 1) {
        commit('CATEGORY_LIST', result.data); // 提交 mutation 保存数据
        return result.data;
      }
      return false;
    },
    // 获取三级分类列表
    async getSanCatList({ commit }) {
      const result = await getSanCatList(); // 调用 API 获取分类列表
      if (result.code === 1) {
        commit('SANCAT_LIST', result.data); // 提交 mutation 保存数据
        return result.data;
      }
      return false;
    },
    // 意见反馈弹窗控制
    showFeedbackModal({ commit }, data) {
      commit('FEEDBACK_MODAL', data);
    },
    // 一键举报弹窗控制
    showReportModal({ commit }, data) {
      commit('REPORT_MODAL', data);
    },
  },
  mutations: {
    // 设置基本信息
    CONFIG(state, payload) {
      state.common = payload;
    },
    SET_REGIONS(state, payload) {
      state.regions = payload;
    },
    // 设置友情链接
    SET_LINKS(state, payload) {
      state.links = payload;
    },
    // 设置底部统计
    SET_STAT(state, payload) {
      state.stat = payload;
    },
    // 设置产品分类列表
    CATEGORY_LIST(state, payload) {
      state.categoryList = payload;
    },
    // 设置三级分类列表
    SANCAT_LIST(state, payload) {
      state.VUEX_sanCatList = payload;
    },
    // 意见反馈弹窗控制
    FEEDBACK_MODAL(state, data) {
      state.isShowFeedbackModal = data;
    },
    // 意见反馈弹窗控制
    REPORT_MODAL(state, data) {
      state.isShowReportModal = data;
    },
  },
};
